import { Player } from "@minecraft/server";
import { PlayerGroupSet } from "@sapi-game/gamePlayer/groupSet";
import { AutoStopState } from "@sapi-game/gameState/common/autoStop";
import { pixelPartyContext } from "./context";
import { PixelPartyMap } from "./map";
import { pixelPartyModule } from "./module";
import { PixelPartyMainState } from "./states/mainState";
import { backHub } from "@/base/func";
import { PixelPartyCountDownState } from "./states/countDown";
import { Hub } from "@/func";

export class PixelParty extends pixelPartyModule.Engine<{
    players: Player[];
    map: PixelPartyMap;
}> {
    protected override buildContext(config: {
        players: Player[];
        map: PixelPartyMap;
    }): pixelPartyContext {
        return new pixelPartyContext(
            this.groupBuilder.fromPlayers(config.players),
            { map: config.map }
        );
    }

    override onStart(): void {
        this.pushState(AutoStopState, {
            groupSet: new PlayerGroupSet([this.context.players]),
            immediateDie: true,
            onLeave(p) {
                this.groupSet.sendMessage(`${p.name}离开了色盲派对`);
            },
        });
        this.pushState(PixelPartyCountDownState);
    }

    override onStop(): void {
        this.context.map.defaultStructure.place();
        this.context.players.forEach((p) => Hub(p.player));
    }
}
